-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSend-PPErrorReport.ps1
84 lines (65 loc) · 2.25 KB
/
Send-PPErrorReport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<#
$Metadata = @{
Title = "Send PowerShell PowerUp Error Report"
Filename = "Send-PPErrorReport.ps1"
Description = "Sends a default error Report based on the PowerShell PowerUp configurations."
Tags = "powershell, profile, error, report"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-08-14"
LastEditDate = "2013-10-21"
Url = ""
Version = "1.1.2"
License = @'
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Send-PPErrorReport{
<#
.SYNOPSIS
Sends a default error Report based on the PowerShell PowerUp configurations.
.DESCRIPTION
Sends a default error Report based on the PowerShell PowerUp configurations.
.PARAMETER FileName
The name of the PowerShell PowerUp config file.
.PARAMETER ScriptName
The name of the PowerShell PowerUp script that throws the error.
.PARAMETER ClearErrorVariable
Clear PowerShell error variable.
.EXAMPLE
PS C:\> Send-PPErrorReport -FileName "Office365.mail.config.xml" -ScriptName $MyInvocation.InvocationName
.NOTES
The name of the configuration in the config file has to be "ErrorReport"
#>
param(
[Parameter(Mandatory=$true)]
[String]
$FileName,
[Parameter(Mandatory=$true)]
[String]
$ScriptName,
[switch]
$ClearErrorVariable
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
if($Error){
# output existing errors
$Error
# reset
$Body = ""
# create body message
$Error | foreach{$Body += $_.ToString() + $_.InvocationInfo.PositionMessage + "`n`n"}
# Get mail receiver from config file
$Mail = Get-PPConfiguration $PSconfigs.Mail.Filter | %{$_.Content.Mail | where{$_.Name -eq $PSconfigs.Mail.ErrorClass}} | select -first 1
# send mail
Send-MailMessage -To $Mail.ReplyToAddress -From $Mail.FromAddress -Subject ($env:COMPUTERNAME + " " + $ScriptName + " #" + $(get-date -format o)) -Body $Body -SmtpServer $Mail.OutSmtpServer
# clear error variable
if($ClearErrorVariable){$error.clear()}
}
}